package com.woniuxy.controllers;

import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.woniuxy.alipay.AlipayBean;
import com.woniuxy.alipay.AlipayUtile;
import com.woniuxy.alipay.IPayModel;
import com.woniuxy.services.OrderService;
import com.woniuxy.utils.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @Classname PayController
 * @Date 2024/1/9 16:38
 */
@RestController
public class PayController {
    @Autowired
    private IPayModel iPayModel;
    @Autowired
    private OrderService orderService;
    @PostMapping(value = "/order/notify")
    public String notify(String outTradeNo, String subject, String totalAmount, String body) throws AlipayApiException {
        AlipayBean alipayBean = new AlipayBean();
        alipayBean.setOut_trade_no(outTradeNo);
        alipayBean.setSubject(subject);
        alipayBean.setTotal_amount(totalAmount);
        alipayBean.setBody(body);
        String pay =    iPayModel.aliPay(alipayBean);

        return pay;
    }

    @GetMapping(value = "/order/success")
    public ResultVO success(HttpServletRequest request) throws UnsupportedEncodingException, AlipayApiException {

        System.out.println("请求到***********************************");
        // 获取支付宝GET过来反馈信息
//        Map<String, String> params = new HashMap<String, String>();
//        Map<String, String[]> requestParams = request.getParameterMap();
//        for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext();) {
//            String name = (String) iter.next();
//            String[] values = (String[]) requestParams.get(name);
//            String valueStr = "";
//            for (int i = 0; i < values.length; i++) {
//                valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
//            }
//            // 乱码解决
//            valueStr = new String(valueStr.getBytes("utf-8"), "utf-8");
//            params.put(name, valueStr);
//        }
//
//        System.out.println(params);//查看参数都有哪些
////        boolean signVerified = AlipaySignature.rsaCheckV1(params, publicKey, charset, signType); // 调用SDK验证签名
//        //验证签名通过
//        if(signVerified){
//            // 商户订单号
//            String out_trade_no = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"), "UTF-8");

//            // 支付宝交易号
//            String trade_no = new String(request.getParameter("trade_no").getBytes("ISO-8859-1"), "UTF-8");
//
//            // 付款金额
//            String total_amount = new String(request.getParameter("total_amount").getBytes("ISO-8859-1"), "UTF-8");
//
//
//            //支付成功，修复支付状态
//            return "ok";//跳转付款成功页面
//        }else{
//            return "no";//跳转付款失败页面
//        }

            String out_trade_no = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"), "UTF-8");
            orderService.updateByonum(out_trade_no);
            return ResultVO.success("ok");

    }
}
